<template>
  <Layer :layer="layer" @confirm="submit" ref="layerDom">
    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px" style="margin-right:30px;">
      <el-form-item label="报备人：" prop="userName">
        <el-select v-model="form.userName" clearable placeholder="请选择报备人">
          <el-option v-for="item in userData" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间：" prop="startDate">
        <el-date-picker v-model="form.startDate" type="datetime" placeholder="请选择开始时间" />
      </el-form-item>
      <el-form-item label="结束时间：" prop="endDate">
        <el-date-picker v-model="form.endDate" type="datetime" placeholder="请选择结束时间" />
      </el-form-item>
      <el-form-item label="报备类型：" prop="requestType">
        <el-select v-model="form.requestType" clearable placeholder="请选择状态">
          <el-option v-for="item in requestTypeData" :key="item.value" :label="item.label"
            :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注：" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
      </el-form-item>
    </el-form>
  </Layer>
</template>

<script lang="ts">
import type { LayerType } from '@/components/layer/index.vue'
import type { Ref } from 'vue'
import type { ElFormItemContext } from 'element-plus/lib/el-form/src/token'
import { defineComponent, ref } from 'vue'
import { add, update } from '@/api/attendance/request'
import { userData, requestTypeData } from './enum'
import Layer from '@/components/layer/index.vue'
export default defineComponent({
  components: {
    Layer
  },
  props: {
    layer: {
      type: Object,
      default: () => {
        return {
          show: false,
          title: '',
          showButton: true
        }
      }
    }
  },
  setup(props, ctx) {
    const ruleForm: Ref<ElFormItemContext | null> = ref(null)
    const layerDom: Ref<LayerType | null> = ref(null)
    let form = ref({
      userName: '',
      startDate: '',
      endDate: '',
      requestType: '',
      remark: '',
    })
    const rules = {
      roleName: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
    }

    init()
    function init() { // 用于判断新增还是编辑功能
      if (props.layer.row) {
        form.value = JSON.parse(JSON.stringify(props.layer.row)) // 数量量少的直接使用这个转
        if (form.value.startDate) form.value.startDate = new Date(form.value.startDate)
        if (form.value.endDate) form.value.endDate = new Date(form.value.endDate)
      } else {

      }
    }
    return {
      form,
      rules,
      layerDom,
      ruleForm,
      userData,
      requestTypeData
    }
  },
  methods: {
    submit() {
      if (this.ruleForm) {
        this.ruleForm.validate((valid) => {
          if (valid) {
            let params = this.form
            if (this.layer.row) {
              this.updateForm(params)
            } else {
              this.addForm(params)
            }
          } else {
            return false;
          }
        });
      }
    },
    // 新增提交事件
    addForm(params: object) {
      add(params)
        .then(res => {
          this.$message({
            type: 'success',
            message: '新增成功'
          })
          this.$emit('getTableData', true)
          this.layerDom && this.layerDom.close()
        })
    },
    // 编辑提交事件
    updateForm(params: object) {
      update(params)
        .then(res => {
          this.$message({
            type: 'success',
            message: '编辑成功'
          })
          this.$emit('getTableData', false)
          this.layerDom && this.layerDom.close()
        })
    }
  }
})
</script>

<style lang="scss" scoped></style>